######################################################################
# Time-stamp: <05/07/18 15:15:12 ostolop>
######################################################################
package EP::EPC::Core::Data_Transformation;

use strict;

use EP::Config;
use EP::Common;

use EP::EPC::Core;
use EP::EPC::Core::Data_Transformation::exp;

use base 'EP::EPC::Core::Component';

sub process_component {
  my $self = shift;

  my ($meta,$new_dataset) = $self->process_transformation ( $self->{src_dataset} );

  my $process_result = {
			rows           => { type => "number",
					    label => "Rows after transform",
					    value => $new_dataset->data->matrix->dim(1) },
			columns        => { type => "number",
					    label => "Columns after transform",
					    value => $new_dataset->data->matrix->dim(0) },
			dataset_name   => $new_dataset->{dataset},
			src_dataset_id => $self->{src_dataset}->id,
			dst_dataset_id => $new_dataset->id
		       };

  $process_result = {%$process_result,%$meta};

  $self->{CD}->{process_result} = $process_result;
  return $self->record_analysis_history ();
}

sub process_transformation {
  my $self = shift;

  $self->{session}->status ( "Processing transformation ..." );

  my ($meta, $trf_dataset);

  $_ = $self->{src_dataset}->type;

 SWITCH: {
    /exp/ && do {
      ($meta, $trf_dataset) = EP::EPC::Core::Data_Transformation::exp::process_transformation ( $self, $self->{src_dataset}, $self->{query} );
      last SWITCH;
    };

    /ppi/ && do {
      last SWITCH;
    };

    /affy/ && do {
      throw EP::Common::Error ( -value => AFFY_TRANSFORM_RAW_DATA );
      last SWITCH;
    };
  }

  die "Failed to process transformation" if not $trf_dataset;

  return ($meta, $trf_dataset);
}

sub create_visualization {
  my $self = shift;
  my ($dataset) = @_;

  $self->{session}->status ( "Preparing visualization..." );

  my $grid = new EP::Visualization::Grid ( 6, 3 );

  my $heatmap_data    = $dataset->data->raw_data; # @{$action->{action_metadata}->{heatmap_data}}[@members];
  my @heatmap_V_hooks = sort { $a <=> $b} keys %{$dataset->data->{row}}; #%{$action->{action_metadata}->{cols}};
  my @heatmap_H_hooks = sort { $a <=> $b} keys %{$dataset->data->{column}}; #%{$action->{action_metadata}->{cols}};

  my $V_hook = new EP::Visualization::Hooks ( \@heatmap_V_hooks, $dataset->data->{row} );
  my $H_hook = new EP::Visualization::Hooks ( \@heatmap_H_hooks, $dataset->data->{column} );

  my ($cell_height, $cell_width ) = ( 1, 1 );

  if ( @heatmap_V_hooks < 200 ) {
    $cell_height = 12;
  } elsif ( @heatmap_V_hooks < 500 ) {
    $cell_height = 6;
  }

  if ( @heatmap_H_hooks < 200 ) {
    $cell_width = 12;
  } elsif ( @heatmap_H_hooks < 500 ) {
    $cell_width = 6;
  }

  #create annotation
  my $row_annotate = $dataset->data->{row};
  my @row_annots;
  push @row_annots, $row_annotate->{$_} foreach sort { $a <=> $b} keys %{$row_annotate};

  my $row_annotation = new EP::Visualization::AnnotationGraphic ( data => \@row_annots, 'font-size' => $cell_height, 'no-color-efs' => 1 );
  $row_annotation->setHook ( $V_hook );

  my $col_annotate = $dataset->data->{column};
  my @col_annots;
  push @col_annots, $col_annotate->{$_} foreach sort { $a <=> $b} keys %{$col_annotate};

  my $col_annotation = new EP::Visualization::AnnotationGraphic ( data => \@col_annots, 'font-size' => $cell_width );
  $col_annotation->setHook ( $H_hook );
  $col_annotation->setOrientation(-90);

  my $cluster_heatmap  = new EP::Visualization::HeatmapGraphic ( data => $heatmap_data, cell_height => $cell_height, cell_width => $cell_width );
  $cluster_heatmap->setVerticalHookSet   ( $V_hook );
  $cluster_heatmap->setHorizontalHookSet ( $H_hook );

  my $cluster_lineplot = new EP::Visualization::LineGraphic ( data => $heatmap_data, plot_height => 150, cell_width => $cell_width );
  $cluster_lineplot->setVerticalHookSet   ( $V_hook );
  $cluster_lineplot->setHorizontalHookSet ( $H_hook );

  $row_annotation = undef if $cell_height == 1;
  $col_annotation = undef if $cell_width  == 1;

  $grid->fillCellArray ( [
			  [ undef,           undef, $col_annotation,  undef, $col_annotation   ],
			  [ $row_annotation, undef, $cluster_heatmap, undef, $cluster_lineplot ],
			  [ undef,           undef, undef,            undef, undef             ]
			 ] );

  die "Failed to generate visualization!" if not defined $grid;

  my $vis_format = $self->{current_user}->visualization_format;

  my $svg_output = "vis.$dataset.$$.svg";
  my $vis_output_filename = "vis.$dataset.$$." . ( $vis_format == 1 ? "png"  : "svg"  );

  open  ( SVG,">$EP::Config::h->{TMP}/$svg_output" );
  print SVG $grid->to_svg();
  close ( SVG );

  # if PNG
  if ( $vis_format == 1 ) {
    # convert to PNG
  }

  return ( "$EP::Config::h->{HTML_HREF}/$EP::Config::h->{WWW_TMP}/$vis_output_filename",
	   $grid->getGridWidth() < 600 ? 580 : $grid->getGridWidth(),
	   $grid->getGridHeight() );
}


1;
